package cn.edkuir.coupon.service.impl;

import cn.edkuir.coupon.entity.SeckillSkuRelationEntity;
import cn.edkuir.coupon.service.SeckillSkuRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edkuir.common.utils.PageUtils;
import cn.edkuir.common.utils.Query;

import cn.edkuir.coupon.dao.SeckillSessionDao;
import cn.edkuir.coupon.entity.SeckillSessionEntity;
import cn.edkuir.coupon.service.SeckillSessionService;
import org.springframework.util.CollectionUtils;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }


    //查询最近三天需要秒杀的活动
    @Override
    public List<SeckillSessionEntity> getSessionByTime() {
        //2021-8-10  2021-8-13
//        LocalDate startDay = LocalDate.now();
//        LocalDate endDay = startDay.plusDays(3);
//
        //00:00:00  23:59:59
//        LocalTime min = LocalTime.MIN;
//        LocalTime max = LocalTime.MAX;
//
//        LocalDateTime start = LocalDateTime.of(startDay, min);
//        LocalDateTime end = LocalDateTime.of(endDay, max);
        List<SeckillSessionEntity> sessionEntities = this.list(new QueryWrapper<SeckillSessionEntity>()
                .between("start_time", startTime(), endTime()));
        if(!CollectionUtils.isEmpty(sessionEntities)) {
            return sessionEntities.stream().peek(session -> {
                Long sessionId = session.getId();
                List<SeckillSkuRelationEntity> skus = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>()
                        .eq("promotion_session_id", sessionId));
                session.setRelationSkus(skus);
            }).collect(Collectors.toList());
        }
        return null;
    }


    private String startTime() {
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        return startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }


    private String endTime() {
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now().plusDays(2), LocalTime.MAX);
        return endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}